package com.kevin.model.db;

import java.io.Serializable;

import lombok.Data;

import com.kevin.service.DBTabelService;
import com.kevin.service.EntityService;

@Data
public class DBTableColumnModel implements Serializable {

    private static final long serialVersionUID = 7392984578665152894L;
    private String tableName; // 表名
    private String columnName; // 列名
    private String columnType; // 列类型
    private Integer datasize; // 大小
    private Integer digits; // 精度
    private Boolean nullable; // 是否可为空
    private String remarks; // 注释

    private String javaDataType; // 导出列类型

    private String exportColName;// 导出字段名

    public String getJavaDataType() {
        if (javaDataType == null) {
            if (columnType == null) {
                return "";
            }
            return DBTabelService.dbType2JavaType(columnType.toLowerCase(), digits < 0 ? 0 : digits,
                    datasize > 0 ? datasize : Math.abs(digits));
        } else {
            return javaDataType;
        }
    }

    public String getExportColName() {
        if (exportColName == null) {
            if (columnName == null) {
                return "";
            }
            String expColName = EntityService.parseFieldName(columnName.toLowerCase());
            return expColName == null ? "" : expColName;
        } else {
            return exportColName;
        }
    }

    public String getColumnTypeShow() {
        String columnType = getColumnType();
        if (columnType == null) {
            return "";
        }
        if (!columnType.toLowerCase().equals("date")) {
            if (getDatasize() > 0) {
                String addStr = getDatasize() + "";
                if (getDigits() > 0) {
                    addStr += "," + getDigits();
                }
                columnType += "(" + addStr + ")";
            }
        }
        return columnType;
    }

}
